/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_rt_prog_ctx.h"
#include "nu_stmt_for.h"
#include "nu_stmt_empty.h"
#include "nu_error_codes.h"
#include "nu_lang_defs.h"


/* -------------------------------------------------------------------------- */

namespace nu
{


// -------------------------------------------------------------------------

stmt_for_t::stmt_for_t(
   prog_ctx_t & ctx,
   stmt_t::handle_t init,
   expr_any_t::handle_t condition,
   stmt_t::handle_t increment
)
   :
   stmt_t(ctx),
   _init(init),
   _condition(condition),
   _increment(increment)
{
   syntax_error_if(ctx.compiletime_pc.get_stmt_pos() > 0,
                   "for statement must be first statement of the line");

   ctx.parsing_scope.push(PROGLANG_STMT_FOR);
   ctx.for_loop_metadata.compile_begin(ctx.compiletime_pc, _increment);
}


// -------------------------------------------------------------------------

stmt_for_t::~stmt_for_t()
{
}


// -------------------------------------------------------------------------

stmt_t::stmt_cl_t stmt_for_t::get_cl() const throw()
{
   return stmt_cl_t::FOR_BEGIN;
}


// -------------------------------------------------------------------------

void stmt_for_t::run(rt_prog_ctx_t& ctx)
{
   //  for (..) {
   //   ...
   //  }


   auto & fctxs = ctx.for_loop_metadata;
   auto handle = fctxs.begin_find(ctx.runtime_pc);

   rt_error_code_t::get_instance().throw_if(
      !handle,
      ctx.runtime_pc.get_line(),
      rt_error_code_t::E_INTERNAL,
      PROGLANG_STMT_FOR);

   var_scope_t::handle_t scope =
      ctx.proc_scope.get(proc_scope_t::type_t::LOCAL);

   std::string for_id = "$"PROGLANG_STMT_FOR"$" + ctx.runtime_pc.to_string();

      if (! scope->is_defined(for_id) && _init)
      { 
         _init->run(ctx);
         scope->define(for_id, variant_t());
      }

      if (!static_cast<bool>(_condition->eval(ctx)))
      {
         handle->flag[instrblock_t::EXIT] = true;
         ctx.go_to(handle->pc_end_stmt);
      }
      else
      {
         handle->flag[instrblock_t::EXIT] = false;
         ctx.go_to_next();
      }

      if (_break_for_loop)
      {
         ctx.flag[rt_prog_ctx_t::FLG_END_REQUEST] = true;
         _break_for_loop = false;
      }
   }


   // -------------------------------------------------------------------------

   bool stmt_for_t::notify(const event_t& ev)
   {
      _break_for_loop = ev == event_t::BREAK;
      return _break_for_loop;
   }


   // -------------------------------------------------------------------------

}

/* -------------------------------------------------------------------------- */

